<?php

class Inc_Form_ItemCreate extends Zend_Form
{
	public function init()
	{
		$this->setAction('/catalog/item/create')->setMethod('post');
	
		
		// name fild
		$SallerName = new Zend_Form_Element_Text('SellerName');
		$SallerName->setLabel('SellerName:')
			->setOptions(array('size' => '35'))
			->setRequired(true)
			->addValidators(array('NotEmpty', 'Alpha'))
			->addFilters(array('HtmlEntities', 'StringTrim'));
		$this->addElement($SallerName);			
		
		// email field
		$this->addElement('text', 'SellerEmail', array(
				'label' => 'SellerEmail:',
				'size' => 35,
				'required' => true,
				'validator' => array('EmailAddress', true),
				'filter' => array('HtmlEntities', 'StringTrim', 'StringToLower')
		));
		
		// phone field
		
		$SallerTel = new Zend_Form_Element_Text('SellerTel');
		$SallerTel->setLabel('Saller Phone:')
				->setOptions(array('size' => '35'))
				->addValidator('StringLength', false, array('min' => 8))
				->addValidator('Regex', false, array(
						'pattern' => '/^\+[1-9][0-9]{6,30}$/',
						'messages' => array(
								Zend_Validate_Regex::INVALID => 
								'\'%value%\' does not match international format +XXYYZZZZ',
								Zend_Validate_Regex::NOT_MATCH => 
								'\'%value%\' does not match international format +XXYYZZZZ')))
				->addFilters(array('HtmlEntities', 'StringTrim'));
		$this->addElement($SallerTel);

		
		// address fild
		$this->addElement('textarea', 'SellerAddress', array(
				'label' => 'Postall Address:',
				'rows' => 6,
				'cols' => 36, 
				'required' => true,
				'filter' => array('HtmlEntities', 'StringTrim')
		));
		
		// add group Seller Information
		$this->addDisplayGroup(array(
				'SellerName', 'SellerEmail', 'SellerTel', 'SellerAddress'
				), 'contact');
		$this->getDisplayGroup('contact')->setOrder(10)->setLegend('Seller Information');
		// end group 

		// title field
		$this->addElement('text', 'Title', array(
				'label' => 'Title:',
				'size' => 35,
				'required' => true,
				'filter' => array('HtmlEntities', 'StringTrim')
		));

		// name mark field
		$Year = new Zend_Form_Element_Text('Year');
		$Year->setLabel('Year:')
			->setOptions(array('size' => 8, 'length' => 4))
			->setRequired(true)
			->addValidator('Between', false, array(
					'min' => 1700,
					'max' => 2015))
			->addFilters(array('HtmlEntities', 'StringTrim'));
		$this->addElement($Year);
	
		// CountryID field
		$country = new Zend_Form_Element_Select('CountryID');
		$country->setLabel('Country:')
				->setRequired(true)
				->addValidator('Int')
				->addFilters(array('HtmlEntities', 'StringTrim', 'StringToUpper'));
		foreach ($this->getCountrys() as $c)
		{
			$country->addMultiOption($c['CountryID'], $c['CountryName']);
		}
		$this->addElement($country);
		
		// field for denomination
		$this->addElement('text', 'Denomination', array(
				'label' => 'Denomination:',
				'size' => 8,
				'required' => true,
				'validator' => array('Float'),
				'filter' => array('HtmlEntities', 'StringTrim')
		));
		
		// radiobutton for choise type mark
		$type = new Zend_Form_Element_Radio('TypeID');
		$type->setLabel('Type:')
				->setRequired(true)
				->addValidator('Int')
				->addFilters(array('HtmlEntities', 'StringTrim'));
		foreach ($this->getTypes() as $t)
		{
			$type->addMultiOption($t['TypeID'], $t['TypeName']);
		}
		$type->setValue(1);
		$this->addElement($type);
		
		// list for choise mark
		$grade = new Zend_Form_Element_Select('GradeID');
		$grade->setLabel('Grade')
			->setRequired(true)
			->addValidator('Int')
			->addFilters(array('HtmlEntities', 'StringTrim'));
		foreach ($this->getGrades() as $g)
		{
			$grade->addMultiOption($g['GradeID'], $g['GradeName']);
		} 
		$this->addElement($grade);
		
		// minimum price
		$this->addElement('text', 'SalePriceMin', array(
				'label' => 'Sale price (min):',
				'size' => 8,
				'required' => true,
				'validator' => array('Float'),
				'filter' => array('HtmlEntities', 'StringTrim')
		));
		
		// maximum price
		$this->addElement('text', 'SalePriceMax', array(
				'label' => 'Sale price (max):',
				'size' => 8,
				'required' => true,
				'validator' => array('Float'),
				'filter' => array('HtmlEntities', 'StringTrim')
		));
		
		// description fild
		$this->addElement('textarea', 'Description', array(
				'label' => 'Description:',
				'rows' => 6,
				'cols' => 26,
				'required' => true,
				'filter' => array('HtmlEntities', 'StringTrim', 'StripTag')
		));
		
		// add group Mark Information
		$this->addDisplayGroup(array(
				'Title', 'Year', 'CountryID', 'Denomination', 'TypeID', 'GradeID',
				'SalePriceMin', 'SalePriceMax', 'Description' 
		), 'item');
		$this->getDisplayGroup('item')->setOrder(20)->setLegend('Item Information');
		// end group
		
		
		// captcha
		$this->addElement('captcha', 'captcha', array(
				'captcha' => array(
		
						'captcha' => 'Image',
						'wordLen' => 3,
						'timeOut' => 300,
						'width' => 150,
						'height' => 100,
						'imgUrl' => '/captcha',
						'imgDir' => APPLICATION_PATH. '/../public/captcha',
						'font' => APPLICATION_PATH. '/../public/fonts/LiberationSans-Regular.ttf'
				)
		));
		
		
		// add group Captcha
		$this->addDisplayGroup(array('captcha'), 'verification');
		$this->getDisplayGroup('verification')->setOrder(30)->setLegend('Verification Code');
		// end group
		
		
		// add field files
		$images = new Zend_Form_Element_File('images');
		$images->setMultiFile(3)
			->addValidator('isImage')
			->addValidator('Size', false, '204800')
			->addValidator('Extension', false, 'jpg,png,gif')
			->addValidator('ImageSize', false, array(
					'minwidth' => 150,
					'minheight' => 150,
					'maxwidth' => 150,
					'maxheight' => 150))
			->setValueDisabled(true);
		$this->addElement($images);
		
		// group files
		$this->addDisplayGroup(array('images'), 'files');
		$this->getDisplayGroup('files')
			->setOrder(40)
			->setLegend('Images');
		
		// submit button
		$this->addElement('submit', 'submit', array(
				'label' => 'GO',
				'order' => 100
		));
	}
	
	public function getCountrys()
	{
		$query = Doctrine_Query::create()
			->from('Zend_Model_Country c');
		return $query->fetchArray();
	}

	public function getTypes()
	{
		$query = Doctrine_Query::create()
		->from('Zend_Model_Type t');
		return $query->fetchArray();
	}
	
	public function getGrades()
	{
		$query = Doctrine_Query::create()
		->from('Zend_Model_Grade g');
		return $query->fetchArray();
	}
}
